package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    public CourseService courseService;

    /*角色列表查询*/
    @GetMapping
    public Result getCourses(String name, Integer subject, Integer target,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("课程列表查询: name={}, subject={}, target={},page={},pageSize={} " , name, subject, target,page,pageSize  );
        PageResult<Course> pageResult =courseService.list(name,subject,target,page,pageSize);
        return Result.success(pageResult);
    }

    /*删除角色*/
    @DeleteMapping("/{id}")
    public Result deleteCourse( Integer id){
        log.info("删除课程{}" , id);
        courseService.deleteById(id);
        return Result.success();
    }

    /*新增角色*/
    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("新增课程: {} " , course);
        courseService.add(course);
        return Result.success();
    }

    /*根据ID查询*/
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询课程: id={} " , id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    /*修改角色*/
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改课程: {} " , course);
        courseService.update(course);
        return Result.success();
    }

    /*查询全部*/
    @GetMapping("/list")
    public Result getAllList(){
        log.info("查询全部课程");
        List<Course> list = courseService.getAllList();
        return Result.success(list);
    }

    /*7. 根据学科查询课程*/
    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject){
        log.info("根据学科查询课程: subject={} " , subject);
        List<Course> course = courseService.getBySubject(subject);
        return Result.success(course);
    }
}
